from django.http import HttpResponse
from django.views import View
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from book.models import BookInfo
from book.serializers_2 import BookSerializer


class BookViewSet(ModelViewSet):
    queryset = BookInfo.objects.all()
    serializer_class = BookSerializer

    def list(self, request):
        return super().list(request)

    def latest(self, request):
        """
        返回最新的图书信息
        """
        book = BookInfo.objects.latest('id')
        serializer = self.get_serializer(book)
        return Response(serializer.data)

    def read(self, request, pk):
        """
        修改图书的阅读量数据
        """
        book = self.get_object()
        book.bread = request.data.get('read')
        book.save()
        serializer = self.get_serializer(book)
        return Response(serializer.data)

